<?php
/**
 * Copyright 2007 Melange.
 *
 * This file is part of Melange_Framework
 *
 * PHP-HTTP is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-HTTP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-HTTP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package http
 *
 */


// ------------------------------------------------------------------------


/**
 * OutputBuffer class
 *
 * A http response class
 *
 * @category    Melange Framework
 * @package     http
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class OutputBuffer {


    /**
     * Logger.
     */
    protected $log;


    // ----------------------------------------------------- Instance Variables


    /**
     * Committed flag.
     */
    protected $committed = false;


    /**
     * Associated response.
     */
    private $response;


    /**
     * Suspended flag. All output bytes will be swallowed if this is true.
     */
    private $suspended = false;


    /**
     * Do a flush on the next operation.
     */
    private $doFlush = false;


    /**
     * Flag which indicates if the output buffer is closed.
     */
    private $closed = false;


    /**
     * State of the output buffer.
     */
    private $initial = true;


    // ------------------------------------------------------------ Constructor


    /**
     * Construct a new outputbuffer.
     *
     */
    public function __construct() {

        $this->log = LoggerManager::getLogger("OutputBuffer");

        if($this->log->isDebugEnabled())
            $this->log->debug("construct()");

        ob_start(); // array(&$this, 'flush')
        ob_implicit_flush(false);

    }


    // ------------------------------------------------------------- Properties


    /**
     * Is the response output closed ?
     *
     * @return closed flag value
     */
    public function isClosed() {
        return $this->closed;
    }


    /**
     * Is the response output commited ?
     *
     * @return commited flag value
     */
    public function isCommited() {
        return $this->committed;
    }


    // --------------------------------------------------------- Public Methods


    /**
     * Close this outputbuffer.
     *
     */
    public function close() {

        if($this->log->isDebugEnabled())
            $this->log->debug(" close()");

        if ($this->closed)
            return;

        if ($this->suspended)
            return;

        ob_end_clean();

    }


    /**
     * Commit outputbuffer
     *
     */
    public function commit() {

        if($this->log->isDebugEnabled())
            $this->log->debug(" commit()");

        $this->committed = true;

        ob_end_flush();

    }


    /**
     * Flush the outputbuffer
     *
     * @return String content of this outputbuffer
     */
    public function flush() {

        if($this->log->isDebugEnabled())
            $this->log->debug(" flush()");

        return ob_flush();

    }


    /**
     * Is this a new outputbuffer ?
     *
     * @return boolean
     */
    public function isNew() {
        return ($this->getCharsWritten() == 0);
    }


    /**
     * Reset the outputbuffer.
     *
     */
    public function reset() {

        if($this->log->isDebugEnabled())
            $this->log->debug(" reset()");

        $this->initial = true;

        ob_clean();

    }


    // ------------------------------------------------- Chars Handling Methods


    /**
     * Get outputbuffer size.
     *
     * @return int size of this outputbuffer
     */
    public function getCharsWritten() {
        return ob_get_length();
    }


    /**
     * Write characters to outputbuffer
     * (same as <code>echo ''</code>)
     *
     * @param String $c
     */
    public function write($c) {

        if ($this->suspended)
            return;

        echo $c;

    }

}

?>